package cn.tannn.ops.nginx.controller.vo;

import com.alibaba.fastjson.JSON;
import com.aliyun.sdk.service.alidns20150109.models.DescribeDomainRecordsResponseBody;
import com.huaweicloud.sdk.dns.v2.model.ShowRecordSetByZoneResp;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * access拥有的域名解析记录信息
 *
 * @author <a href="https://t.tannn.cn/">tan</a>
 * @version V1.0
 * @date 2024/9/20 上午10:44
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DnsDomainRecord {
    @Schema(description = "主机记录(域名前缀)")
    String rr;
    @Schema(description = "主域名")
    String domain;
    @Schema(description = "记录ID")
    String recordId;
    @Schema(description = "记录类型(指解析记录的用途)")
    String type;
    @Schema(description = "状态(解析记录在云解析DNS中的启用情况)[DISABLE,ENABLE]")
    String status;
    @Schema(description = "记录值(一般是填写服务器的IP地址)")
    String value;
    @Schema(description = "指解析结果在Local DNS中的缓存时间/秒")
    Long ttl;
    @Schema(description = "解析请求来源(ISP)(指域名访问者所在的地区和使用的运营商网络)[default]")
    String line;
    @Schema(description = "备注")
    String remark;
    @Schema(description = "华为云的")
    String zoneId;

    public static DnsDomainRecord toDnsDomainRecord(DescribeDomainRecordsResponseBody.Record rc){
       return DnsDomainRecord.builder()
                .domain(rc.getDomainName())
                .recordId(rc.getRecordId())
                .type(rc.getType())
                .line(rc.getLine())
                .value(rc.getValue())
                .status(rc.getStatus())
                .remark(rc.getRemark())
                .ttl(rc.getTTL())
                .rr(rc.getRr())
                .build();
    }

    public static DnsDomainRecord toDnsDomainRecord(ShowRecordSetByZoneResp rc,String domain){
        String rr;
        if( rc.getName().equals(domain)){
            rr = "@";
        }else {
            rr = rc.getName().replace("."+domain, "");
        }
        return DnsDomainRecord.builder()
                .domain(domain)
                .recordId(rc.getId())
                .type(rc.getType())
                .line(rc.getLine())
                .value(JSON.toJSONString(rc.getRecords()))
                .status(rc.getStatus())
                .remark(rc.getDescription())
                .ttl(rc.getTtl().longValue())
                .rr(rr)
                .zoneId(rc.getZoneId())
                .build();
    }
}
